<template>
  <div class="login">
    <!-- 左 -->
    <div class="left">
      <div class="header">
        <img class="left-logo" src="@/assets/img/logo.png" alt />
        <span class="t1">黑马面面</span>
        <div class="line"></div>
        <span class="t2">用户登录</span>
      </div>
      <!-- el-form
      model:整个值的绑定
      label-width--文本标签宽度
      el-form-item
      lable；标题

      ：wq---合并分支弹出框输入

      基本验证，手机号码验证后面学
      -->
      <el-form class="form" :rules="rules" :model="form" ref="rluesform">
        <el-form-item prop="phone">
          <el-input prefix-icon="el-icon-user" placeholder="请输入手机号" v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            :show-password="true"
            placeholder="请输入密码"
            v-model="form.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input prefix-icon="el-icon-key" placeholder="请输入验证码" v-model="form.code"></el-input>
            </el-col>
            <el-col :span="8">
              <!-- 更新验证码 -->
              <img @click="updatecode" class="code-img" :src="codeUrl" alt />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass">
            我已阅读和同意
            <el-link type="primary">用户协议</el-link>和
            <el-link type="primary">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit" class="btn">登录</el-button>
          <br />
          <el-button type="primary" class="btn" @click="showregister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 右 -->
    <img src="@/assets/img/login_right.png" alt />
    <register ref="register"></register>
  </div>
</template>

<script>
// 1导入
import register from "./register";
import { toLogin } from "@/api/login.js";
import { saveLocal, getLocal } from "@/utils/local.js";

// 2注册

// 3标签
export default {
  components: {
    register
  },
  data() {
    return {
      // bol: false,
      codeUrl: process.env.VUE_APP_URL + "/captcha?type=login",
      form: {
        phone: "",
        password: "",
        code: "",
        // isPass: "",
        isPass: "" //非空验证，加不加参数不影响
      },
      rules: {
        phone: [
          { required: true, message: "请输入正确号码", trigger: "change" },
          {
            min: 11,
            max: 11,
            message: "请输入正确格式号码",
            trigger: "change"
          },
          {
            validator: (rule, value, callback) => {
              let _reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (_reg.test(value)) {
                callback();
              } else {
                callback(new Error("手机的格式不对，重新输入"));
              }
            },
            trigger: "change"
          }
        ],
        password: [
          { required: true, message: "请输入正确mm", trigger: "change" },
          { min: 6, max: 12, message: "请输入正确格式密码", trigger: "change" }
        ],
        code: [
          { required: true, message: "请输入正确的验证码", trigger: "change" },
          {
            min: 4,
            max: 4,
            message: "请输入正确的验证码",
            trigger: "change"
          }
        ],
        isPass: [
          { required: true, message: "请勾选协议", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              if (value === true) {
                callback();
              } else {
                callback(new Error("请勾选协议"));
              }
            },
            trigger: "change"
          }
        ]
      }
    };
  },
  created() {
    if (getLocal()) {
      this.$router.push("./layout");
    }
  },
  methods: {
    // 登录提
    submit() {
      this.$refs.rluesform.validate(result => {
        // Window.console.log(result);
        if (result) {
          // 弹窗提示 $message
          /*
          this.$message("提示内容")
          this.$message.info("提示内容")
          this.$message.success("成功提示")
          this.$message.error("出错的提示")
          this.$message.warning("警告提示")
          this.$message({
            type:"success",
             message: '恭喜你，这是一条成功消息',
          })
          */
          // this.$message.success('验证通过')
          toLogin(this.form).then(res => {
            this.$message.success("登陆成功");
            // Window.console.log(res);

            // 出于方便以后维护，我们一般会把这种token操作存储为一个工具型方法
            saveLocal(res.data.token);
            // window.localStorage.setItem('token', res.data.token)
            // 路由跳转
            this.$router.push("/layout");
            window.console.log("res登陆成功:", res.data.token);
          });
        } else {
          this.$message({
            type: "error",
            message: "验证失败"
          });
        }
      });
    },

    // 刷新图形码
    updatecode() {
      // 1静默刷新
      // this.bol = false;
      // this.$nexttick(() => {
      //   this.bol = "true";
      // });
      // 2Date.now
      this.codeUrl =
        process.env.VUE_APP_URL +
        "/captcha?type=login&rewr" +
        Math.random() * 23;
    },
    showregister() {
      this.$refs.register.isShow = true;
    },
    login(forname) {
      this.$refs[forname].validate(result => {
        if (result) {
          // 弹窗提示--$message
          // this.$message("文本提示内容")---默认
          // this.$message.success("成功提示")---成功
          // this.$message.error("失败提示")---失败
          // this.$message.warning("警告提示")---警告
          // 复杂一些的写法解释
          // this.$message({
          //   type: "success",
          //   message: "成功提示"
          // });

          // alert("登录成功");
          this.$message.success("成功提示");
        } else {
          // alert("登录失败");
          this.$message.error("失败提示");
        }
      });
    }
  }
};
</script>

<style lang='less'>
.login {
  background: linear-gradient(to right, #1494fa, #01c6fa);
  display: flex;
  height: 100%;
  /* 垂直居中 */
  align-items: center;
  /* 水平布局 */
  justify-content: space-around;
  .left {
    width: 478px;
    height: 550px;
    background-color: #f5f5f5;
    // 统一内边距
    padding: 48px;
    // 上下居中对齐
    .header {
      align-items: center;
      // 保持同一行
      display: flex;
      .left-logo {
        width: 22px;
        height: 17px;
        margin-right: 20px;
      }
      .t1 {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
      .line {
        width: 1px;
        height: 28px;
        background: #c7c7c7;
        margin: 0 15px;
      }
      .t2 {
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
    }
    .form {
      margin-top: 20px;
      .code-img {
        width: 100%;
        height: 40px;
      }
      .btn {
        width: 100%;
        display: block;
      }
    }
  }
}
</style>